const app = getApp()
Page({
    data: {
        inputValue: '',
        randomNum: 0,
        inputLength: false,
        textareaValue: '',
        textareaLength: 0,
        textareaType: true,
        tempFilePaths: [],
        tokenID: '',
        productInfo: {},
        actionSheetHidden: true,
        actionSheetItems: [{
                bindtap: 'Menu1',
                iocntype: true,
                txt: '从手机相册选择'
            },
            {
                bindtap: 'Menu2',
                iocntype: false,
                txt: '拍摄'
            }
        ],
        menu: ''
    },
    onLoad: function(options) {
        var _this = this;
        var random = (Math.random() * 10000000).toString(16).substr(0, 4) + '-' + (new Date()).getTime() + '-' + Math.random().toString().substr(2, 5);
        _this.setData({
            tokenID: app.data.tokenID,
            randomNum: random
        })
    },
    bindKeyInput: function(event) {
        var _this = this,
            inputValue = event.detail.value;
        this.setData({
            inputValue: inputValue
        })
    },
    textareachange: function(event) {
        var _this = this,
            textareaValue = event.detail.value,
            textareaLength = textareaValue.length,
            textareaType;
        if (textareaLength > 1000) {
            textareaType = false
        } else {
            textareaType = true
        }
        _this.setData({
            textareaValue: textareaValue,
            textareaLength: textareaLength,
            textareaType: textareaType
        })
    },
    actionSheetTap: function() {
        this.setData({
            actionSheetHidden: !this.data.actionSheetHidden
        })
    },
    actionSheetbindchange: function() {
        this.setData({
            actionSheetHidden: !this.data.actionSheetHidden
        })
    },
    bindMenu1: function() {
        var _this = this;
        _this.setData({
            menu: 1,
            actionSheetHidden: !_this.data.actionSheetHidden
        })
        wx.chooseImage({
            count: 3,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: function(res) {
                var tempFilePaths = res.tempFilePaths;
                wx.showToast({
                    title: '正在上传...',
                    icon: 'loading',
                    mask: true,
                    duration: 10000
                })
                for (var i = 0; i < tempFilePaths.length; i++) {
                    wx.uploadFile({
                        url: app.data.api + '/questions/picupload',
                        filePath: res.tempFilePaths[i],
                        name: 'file',
                        formData: {
                            "uuid": _this.data.randomNum,
                            "token": _this.data.tokenID,
                        },
                        header: {
                            "Content-Type": "multipart/form-data"
                        },
                        success: function(res) {
                            var obj = res.data;
                            var data = JSON.parse(obj)
                            var FilePaths = _this.data.tempFilePaths;
                            var json = {}
                            json.sysid = data.data.sysid;
                            json.imgUrl = data.data.imgUrl;
                            FilePaths.push(json)
                            _this.setData({
                                tempFilePaths: FilePaths
                            })
                            wx.hideToast();
                        },
                        fail: function(res) {
                            wx.hideToast();
                            wx.showModal({
                                title: '错误提示',
                                content: '上传图片失败',
                                showCancel: false,
                                success: function(res) {}
                            })
                        }
                    });
                }
            }
        });
    },
    bindMenu2: function() {
        var _this = this;
        _this.setData({
            menu: 2,
            actionSheetHidden: !_this.data.actionSheetHidden
        })
        wx.chooseImage({
            count: 3,
            sizeType: ['compressed'],
            sourceType: ['camera'],
            success: function(res) {
                var tempFilePaths = res.tempFilePaths;
                wx.showToast({
                    title: '正在上传...',
                    icon: 'loading',
                    mask: true,
                    duration: 10000
                })
                for (var i = 0; i < tempFilePaths.length; i++) {
                    wx.uploadFile({
                        url: app.data.api + '/questions/picupload',
                        filePath: res.tempFilePaths[i],
                        name: 'file',
                        formData: {
                            "uuid": _this.data.randomNum,
                            "token": _this.data.tokenID,
                        },
                        header: {
                            "Content-Type": "multipart/form-data"
                        },
                        success: function(res) {
                            var obj = res.data;
                            var data = JSON.parse(obj)
                            var FilePaths = _this.data.tempFilePaths;
                            var json = {}
                            json.sysid = data.data.sysid;
                            json.imgUrl = data.data.imgUrl;
                            FilePaths.push(json)
                            _this.setData({
                                tempFilePaths: FilePaths
                            })
                            wx.hideToast();
                        },
                        fail: function(res) {
                            wx.hideToast();
                            wx.showModal({
                                title: '错误提示',
                                content: '上传图片失败',
                                showCancel: false,
                                success: function(res) {}
                            })
                        }
                    });
                }
            }
        });
    },
    imgshow: function(event) {
        var _this = this;
        var src = event.currentTarget.dataset.src;
        var index = event.currentTarget.dataset.index;
        var imgs = _this.data.tempFilePaths;
        var imgList = [];
        for (var i = 0; i < imgs.length; i++) {
            imgList.push(imgs[i].imgUrl)
        }
        wx.previewImage({
            current: src,
            urls: imgList
        })
    },
    deleteImage: function(event) {
        var _this = this,
            index = event.currentTarget.dataset.index,
            sysid = event.currentTarget.dataset.sysid,
            imgList = _this.data.tempFilePaths;
        wx.showToast({
            title: '删除中...',
            icon: 'loading',
            mask: true,
            duration: 10000
        })
        wx.request({
            url: app.data.api + '/questions/deleteimg',
            data: {
                "token": _this.data.tokenID,
                "sysid": sysid
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    imgList.splice(index, 1);
                    _this.setData({
                        tempFilePaths: imgList
                    })
                    wx.hideToast();
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        image: '../../images/warning.png',
                        duration: 800,
                    })
                }
            }
        })
    },
    submitRelease: function() {
        var _this = this;
        if (_this.data.inputValue == '') {
            wx.showToast({
                title: '话题标题为空',
                image: '../../images/warning.png',
                duration: 800,
                mask: false,
                success: function() {},
                fail: function() {},
                complete: function() {}
            })
            return false
        }
        if (_this.data.textareaValue == '') {
            wx.showToast({
                title: '话题描述为空',
                image: '../../images/warning.png',
                duration: 800,
                mask: false,
                success: function() {},
                fail: function() {},
                complete: function() {}
            })
            return false
        }
        wx.showToast({
            title: '正在发布...',
            icon: 'loading',
            mask: true,
            duration: 10000
        })
        wx.request({
            url: app.data.api + '/questions/savequestions',
            data: {
                "title": _this.data.inputValue,
                "content": _this.data.textareaValue,
                "token": _this.data.tokenID,
                "uuid": _this.data.randomNum
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    wx.hideToast();
                    wx.navigateBack({
                        delta: 2
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        image: '../../images/warning.png',
                        duration: 800,
                        mask: false,
                        success: function() {},
                        fail: function() {},
                        complete: function() {}
                    })
                }
            }
        })
    }
})